//
//  GWLightViewController.m
//  Glowalker
//
//  Created by 王华磊 on 2017/10/3.
//  Copyright © 2017年 Haley. All rights reserved.
//

#import "GWLightViewController.h"
#import "BluetoothManager.h"

#import "NSString+QCExtension.h"
#import "UIImageView+ATGetColor.h"

@interface GWLightViewController ()

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView       *gradientImageView;
@property (strong, nonatomic) UIImageView       *pointImageView;
@property (strong, nonatomic) UISwitch          *switchView;
@property (strong, nonatomic) UIView            *containView;
@property (strong, nonatomic) IBOutlet UIView   *bottomView;
@property (strong, nonatomic) IBOutlet UISlider *slider;

@property (assign, nonatomic)   float         red;
@property (assign, nonatomic)   float         green;
@property (assign, nonatomic)   float         blue;


@property (strong, nonatomic) NSMutableArray      *btns;

@property (strong, nonatomic) NSArray            *rgbColors;

@end

@implementation GWLightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupLanguage];
    
    self.red = 1;
    self.green = 1;
    self.blue = 1;
    
    [self initScrollView];
    
    [self initBottomView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [BluetoothManager standardManager].lampStateBlock = ^(BOOL isON){
        self.switchView.on = isON;
    };
    [BluetoothManager standardManager].ConnectedPeripheralBlock = ^(BOOL isConnected){
        [[BluetoothManager standardManager] writCommad:@"F0"];
    };
    
    [[BluetoothManager standardManager] writCommad:@"F0"];
}
    
- (void)setupLanguage
{
    self.navigationItem.title = showText(@"灯光");
}

- (void)initScrollView
{
    CGFloat imgViewW = kWidth * 2 / 3;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth/6, 20, imgViewW, imgViewW)];
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickColorBg:)];
    [imageView addGestureRecognizer:gesture];
    self.gradientImageView = imageView;
    self.gradientImageView.image = [UIImage imageNamed:@"light_gradient"];
    
    CGFloat choseImgW = 20;
    UIImageView *pointImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, choseImgW, choseImgW)];
    pointImageView.center = CGPointMake(imgViewW*0.5, imgViewW*0.5);
    pointImageView.image = [UIImage imageNamed:@"light_chose"];
    pointImageView.userInteractionEnabled = YES;
    self.pointImageView = pointImageView;
    [self.gradientImageView addSubview:self.pointImageView];
    [self.view addSubview:self.gradientImageView];
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.pointImageView addGestureRecognizer:panGesture];
    
    
    CGFloat srollViewH = kHeight-64-49-30 - imgViewW;
    self.scrollView.frame = CGRectMake(0, imgViewW, kWidth, srollViewH);
    self.scrollView.backgroundColor = [UIColor clearColor];
//    [self.scrollView addSubview:self.gradientImageView];
    
    self.switchView = [[UISwitch alloc] initWithFrame:CGRectMake(kWidth-70, 20, 50, 30)];
    self.switchView.on = YES;
    [self.switchView addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.switchView];
    
    self.containView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, kWidth, 160)];
    self.containView.backgroundColor = [UIColor clearColor];
    CGFloat imgW = 50;
    CGFloat imgH = imgW;
    CGFloat paddingW = (kWidth-imgW*4)/5;
    CGFloat paddingH = 20;
    NSArray *imageNames = @[@"ico_white",@"ico_yellow",@"ico_red",@"ico_blue",@"ico_green",@"ico_cyan",@"ico_purple",@"ico_orange"];
    NSArray *rgbColors = @[@"1,1,1",@"1,1,0",@"1,0,0",@"0,0,1",@"0,1,0",@"0,1,1",@"1,0,1",@"1,0.65,0"];
    self.rgbColors = rgbColors;
    self.btns = [NSMutableArray arrayWithCapacity:imageNames.count];
    for (int i = 0; i < imageNames.count; i++) {
        int row = i / 4;
        int col = i % 4;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(paddingW + (imgW + paddingW) * col, paddingH + (imgH + paddingH) * row, imgW, imgH)];
        button.tag = i;
        [button setBackgroundImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btns addObject:button];
        [self.containView addSubview:button];
    }
    
    CGFloat contentH = imgViewW + 240;
    self.scrollView.contentSize = CGSizeMake(kWidth, contentH);
    [self.scrollView addSubview:self.containView];
}

- (void)initBottomView
{
    self.bottomView.frame = CGRectMake(0, kHeight-64-49-30, kWidth, 30);
    self.bottomView.backgroundColor = [UIColor clearColor];
    self.slider.frame = CGRectMake(8, 0, kWidth - 16, 30);
    self.slider.minimumValueImage = [UIImage imageNamed:@"brightness_left_new"];
    self.slider.maximumValueImage = [UIImage imageNamed:@"brightness_right_new"];
    [self.slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
}

- (void)switchChange:(UISwitch *)switchView
{
    [[BluetoothManager standardManager] writCommad:@"04"];
}

- (void)panAction:(UIPanGestureRecognizer *)gesture
{
    CGPoint colorPoint = [gesture locationInView:self.gradientImageView];
//    self.pointImageView.center = colorPoint;
    [self.gradientImageView at_getRGBAFromCircleWithPoint:colorPoint completion:^(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha) {
        //移动当前选中颜色
        [UIView animateWithDuration:0.25 animations:^{
            self.pointImageView.center = colorPoint;
        }];
        //变化圆形滑块的颜色
        _red = red;
        _green = green;
        _blue = blue;
        //颜色传输
        float brightness = self.slider.value;
        NSString *rgbCmd = [NSString stringWithFormat:@"03%@",[NSString convertStringToRed:_red*brightness Green:_green*brightness Blue:_blue*brightness]];
        [[BluetoothManager standardManager] writCommad:rgbCmd];
        [[BluetoothManager standardManager] writCommad:@"F0"];
        
        for (UIButton *btn in self.btns) {
            btn.layer.borderWidth = 0;
        }
    }];
}

- (void)clickColorBg:(UIGestureRecognizer *)gesture
{
    CGPoint colorPoint = [gesture locationInView:self.gradientImageView];
    [self.gradientImageView at_getRGBAFromCircleWithPoint:colorPoint completion:^(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha) {
        //移动当前选中颜色
        [UIView animateWithDuration:0.25 animations:^{
            self.pointImageView.center = colorPoint;
        }];
        //变化圆形滑块的颜色
        _red = red;
        _green = green;
        _blue = blue;
        //颜色传输
        float brightness = self.slider.value;
        NSString *rgbCmd = [NSString stringWithFormat:@"03%@",[NSString convertStringToRed:_red*brightness Green:_green*brightness Blue:_blue*brightness]];
        [[BluetoothManager standardManager] writCommad:rgbCmd];
        [[BluetoothManager standardManager] writCommad:@"F0"];
        
        for (UIButton *btn in self.btns) {
            btn.layer.borderWidth = 0;
        }
    }];
}

- (void)sliderChange:(UISlider *)slider
{
    float brightness = slider.value;
    
    NSString *rgbCmd = [NSString stringWithFormat:@"03%@",[NSString convertStringToRed:_red*brightness Green:_green*brightness Blue:_blue*brightness]];
    [[BluetoothManager standardManager] writCommad:rgbCmd];
    [[BluetoothManager standardManager] writCommad:@"F0"];
}

- (void)btnClick:(UIButton *)button
{
    for (UIButton *btn in self.btns) {
        btn.layer.borderWidth = 0;
    }
    
    button.layer.borderColor = [UIColor whiteColor].CGColor;
    button.layer.borderWidth = 1;
    
    NSString *rgbString = self.rgbColors[button.tag];
    NSArray *rgbArray = [rgbString componentsSeparatedByString:@","];
    
    if (rgbArray.count < 3) {
        return;
    }
    self.red = [rgbArray[0] floatValue];
    self.green = [rgbArray[1] floatValue];
    self.blue = [rgbArray[2] floatValue];
    
    float brightness = self.slider.value;
    
    NSString *rgbCmd = [NSString stringWithFormat:@"03%@",[NSString convertStringToRed:_red*brightness Green:_green*brightness Blue:_blue*brightness]];
    [[BluetoothManager standardManager] writCommad:rgbCmd];
    
    [[BluetoothManager standardManager] writCommad:@"F0"];
}

@end
